/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.pojo.HistoricoAluno;

/**
 *
 * @author leonidia
 */
public class HistoricoAlunoDaoImpl implements HistoricoAlunoDao {

    ArrayList<HistoricoAluno> listaHistoricoAluno = new ArrayList<>();
    /**
     * ******Associacao do Historico*********
     */
    ArrayList<Integer> listaIdAluno = new ArrayList<>();
    ArrayList<Integer> listaIdHistorico = new ArrayList<>();
    ArrayList<Integer> listaIdTurma = new ArrayList<>();

    @Override
    public void setHistoricoAluno(ArrayList<HistoricoAluno> historicoAluno) {
        this.listaHistoricoAluno = historicoAluno;
        try {
            carregarAssocHistoricoAluno();
        } catch (IOException ex) {
            Logger.getLogger(HistoricoAlunoDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public ArrayList<HistoricoAluno> getHistoricoAluno() {
        return this.listaHistoricoAluno;
    }

    @Override
    public boolean addHistoricoAluno(HistoricoAluno historicoAluno) {
        if (listaHistoricoAluno.contains(historicoAluno)) {
            return false;
        } else {
            historicoAluno.setId(novoId());
            this.listaHistoricoAluno.add(historicoAluno);
            return true;
        }
    }

    @Override
    public void salvarHistoricoAluno(ArrayList<HistoricoAluno> historicoAluno) throws IOException {
        try (FileWriter writer = new FileWriter("HistoricoAluno.txt", false);
                PrintWriter saida = new PrintWriter(writer, true)) {
            int i;
            for (i = 0; i < historicoAluno.size(); i++) {
                writer.write(historicoAluno.get(i).getId() + ";"
                        + historicoAluno.get(i).getId() + ";" + historicoAluno.get(i).getNota()
                        + ";" + historicoAluno.get(i).getFalta() + "\n");
            }
        }
    }

    @Override
    public ArrayList<HistoricoAluno> carregarHistoricoAluno() throws IOException {
        ArrayList<HistoricoAluno> listaHistoricoaluno;
        listaHistoricoaluno = new ArrayList<>();
        File file = new File("HistoricoAluno.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        try (BufferedReader in = new BufferedReader(new FileReader(file))) {
            String str;
            while ((str = in.readLine()) != null) {
                String[] t = str.split(";");
                HistoricoAluno historicoAluno = new HistoricoAluno(Integer.parseInt(t[0]), Float.parseFloat(t[1]), Integer.parseInt(t[2]));
                listaHistoricoaluno.add(historicoAluno);
            }
        }
        return listaHistoricoaluno;
    }

    @Override
    public boolean contains(HistoricoAluno historicoAluno) {
        Integer i;
        if (this.listaHistoricoAluno.isEmpty()) {
            return false;
        }
        for (i = 0; i < this.listaHistoricoAluno.size(); i++) {
            if ((this.listaHistoricoAluno.get(i).getFalta() == historicoAluno.getFalta())
                    && (this.listaHistoricoAluno.get(i).getNota() == historicoAluno.getNota())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Integer novoId() {
        if (this.listaHistoricoAluno.isEmpty()) {
            return 0;
        }
        return this.listaHistoricoAluno.get(this.listaHistoricoAluno.size() - 1).getId() + 1;
    }

    @Override
    public Integer ultimoId() {
        if (this.listaHistoricoAluno.isEmpty()) {
            return 0;
        }
        return listaHistoricoAluno.get(listaHistoricoAluno.size() - 1).getId();
    }

    /**
     * ***************Metodos de Associacao*****************
     */
    @Override
    public void salvarAssocHistoricoAluno() throws IOException {
        try (FileWriter writer = new FileWriter("AssocHistoricoAluno.txt", false);
                PrintWriter saida = new PrintWriter(writer, true)) {
            int i;
            for (i = 0; i < this.listaIdHistorico.size(); i++) {
                writer.write(this.listaIdAluno.get(i) + ";"
                        + listaIdHistorico.get(i) + ";" + ";" + listaIdTurma.get(i) + "\n");
            }
        }
    }

    @Override
    public void carregarAssocHistoricoAluno() throws IOException {
        ArrayList<HistoricoAluno> listaHistoricoaluno;
        listaHistoricoaluno = new ArrayList<>();
        File file = new File("AssocHistoricoAluno.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        try (BufferedReader in = new BufferedReader(new FileReader(file))) {
            String str;
            while ((str = in.readLine()) != null) {
                String[] t = str.split(";");
                this.listaIdAluno.add(Integer.parseInt(t[0]));
                this.listaIdHistorico.add(Integer.parseInt(t[1]));
                this.listaIdTurma.add(Integer.parseInt(t[2]));
            }
        }
    }

    @Override
    public HistoricoAluno procuraHistorico(Integer idAluno, Integer idTurma) {
        Integer i, j;
        for (i = 0; i < listaHistoricoAluno.size(); i++) {
            if (idAluno == listaIdAluno.get(i) && idTurma == listaIdTurma.get(i)) {
                for (j = 0; j < listaHistoricoAluno.size(); j++) {
                    if (listaHistoricoAluno.get(j).getId() == listaIdHistorico.get(j)) {
                        return listaHistoricoAluno.get(j);
                    }
                }
            }
        }
        return null;
    }
}
